//
//  XMImageToolView.m
//  XiaoMiao
//
//  Created by 23 on 2018/2/5.
//  Copyright © 2018年 smallyou. All rights reserved.
//

#import "XMImageToolView.h"

@interface XMImageToolView ()

@property (weak, nonatomic) IBOutlet UIButton *maskButton;
@property (weak, nonatomic) IBOutlet UIButton *shuiyinButton;
@property (weak, nonatomic) IBOutlet UIButton *confirmButton;
@property (weak, nonatomic) IBOutlet UITextField *attachTextField;
@property (weak, nonatomic) IBOutlet UIButton *backwardButton;
@property (weak, nonatomic) IBOutlet UIButton *forwardButton;
@property (weak, nonatomic) IBOutlet UIView *optionBgView;
@property (weak, nonatomic) IBOutlet UIButton *attachClearBtn;

/**类型状态*/
@property(nonatomic,assign) XMImageToolStatus styleStatus;


@end

@implementation XMImageToolView


+ (instancetype)imageToolView
{
    return [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(XMImageToolView.class) owner:nil options:nil].firstObject;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    
    //默认初始状态
    [self updateUIWithStyleStatus:XMImageToolStatusInitial];

}

- (void)updateUIWithStyleStatus:(XMImageToolStatus )styleStatus
{
    
    self.styleStatus = styleStatus;
    
    if ([self.delegate respondsToSelector:@selector(imageToolView:didStyleStatusChanged:)]) {
        [self.delegate imageToolView:self didStyleStatusChanged:styleStatus];
    }
    
    [self.shuiyinButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.maskButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.shuiyinButton.titleLabel.font = [UIFont systemFontOfSize:12.0];
    self.maskButton.titleLabel.font = [UIFont systemFontOfSize:12.0];
    self.attachClearBtn.hidden = YES;
    switch (styleStatus) {
        case XMImageToolStatusInitial:
        {
            //初识状态
            self.maskButton.hidden = NO;
            self.shuiyinButton.hidden = NO;
            
            self.confirmButton.hidden = YES;
            self.attachTextField.hidden = YES;
            self.backwardButton.hidden = YES;
            self.forwardButton.hidden = YES;
            
            [self.shuiyinButton setImage:[UIImage imageNamed:@"ic_album_shuiyin"] forState:UIControlStateNormal];
            [self.shuiyinButton setTitle:@"文字水印" forState:UIControlStateNormal];
            [self.maskButton setTitle:@"马赛克" forState:UIControlStateNormal];
            self.optionBgView.backgroundColor = [UIColor groupTableViewBackgroundColor];
            
            self.shuiyinButton.selected = NO;
            self.maskButton.selected = NO;
        }
            break;
        case XMImageToolStatusMask:
        {
            //马赛克状态
            self.maskButton.hidden = NO;
            self.shuiyinButton.hidden = NO;
            
            self.confirmButton.hidden = NO;
            self.attachTextField.hidden = YES;
            self.backwardButton.hidden = NO;
            self.forwardButton.hidden = NO;
 
            [self.shuiyinButton setImage:[UIImage imageNamed:@"ic_album_eraser"] forState:UIControlStateNormal];
            [self.shuiyinButton setTitle:@"橡皮擦" forState:UIControlStateNormal];
            [self.maskButton setTitle:@"马赛克" forState:UIControlStateNormal];
            self.optionBgView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        }
            break;
        case XMImageToolStatusAttach:
        {
            self.maskButton.hidden = YES;
            self.shuiyinButton.hidden = YES;
            self.attachClearBtn.hidden = NO;
            self.confirmButton.hidden = NO;
            self.attachTextField.hidden = NO;
            self.backwardButton.hidden = YES;
            self.forwardButton.hidden = YES;
            
            [self.shuiyinButton setImage:[UIImage imageNamed:@"ic_album_eraser"] forState:UIControlStateNormal];
            self.optionBgView.backgroundColor = [UIColor whiteColor];
            
        }
            break;
        case XMImageToolStatusClearAttach:
        {
            self.maskButton.hidden = YES;
            self.shuiyinButton.hidden = YES;
            self.attachClearBtn.hidden = NO;
            self.confirmButton.hidden = NO;
            self.attachTextField.hidden = NO;
            self.backwardButton.hidden = YES;
            self.forwardButton.hidden = YES;
            
            [self.shuiyinButton setImage:[UIImage imageNamed:@"ic_album_eraser"] forState:UIControlStateNormal];
            self.optionBgView.backgroundColor = [UIColor whiteColor];
            
        }
            break;
        case XMImageToolStatusEraser:
        {
            [self.shuiyinButton setImage:[UIImage imageNamed:@"ic_album_eraser_blue"] forState:UIControlStateNormal];
        }
            break;
        default:
            break;
    }
}

#pragma mark - action
//马赛克
- (IBAction)actionMask:(UIButton *)sender {
    sender.selected = !sender.isSelected;
    
    //马赛克
    if (sender.isSelected) {
        [self updateUIWithStyleStatus:XMImageToolStatusMask];
    }else{
        [self updateUIWithStyleStatus:XMImageToolStatusInitial];
    }
}

//水印
- (IBAction)actionShuiyin:(UIButton *)sender {
    
    if (self.styleStatus == XMImageToolStatusMask || self.styleStatus == XMImageToolStatusEraser) {
        //如果是橡皮擦按钮
        self.shuiyinButton.selected = !self.shuiyinButton.isSelected;
    
        if (self.shuiyinButton.isSelected) {
            //橡皮擦模式
            [self updateUIWithStyleStatus:XMImageToolStatusEraser];
        }else{
            //马赛克模式
            [self updateUIWithStyleStatus:XMImageToolStatusMask];
        }
    }else{
        //如果是水印按钮
        self.maskButton.selected = NO;
        self.shuiyinButton.selected = NO;
        
        //水印
        [self updateUIWithStyleStatus:XMImageToolStatusAttach];
        
    }
}
//清除文字水印
- (IBAction)attachClearACtion:(UIButton *)sender {
    
    [self updateUIWithStyleStatus:XMImageToolStatusClearAttach];

}

//确认
- (IBAction)actionConfirm:(UIButton *)sender {
    
    if ([self.attachTextField isFirstResponder]) {
        [self.attachTextField resignFirstResponder];
    }
    
    [self updateUIWithStyleStatus:XMImageToolStatusInitial];
    
    if ([self.delegate respondsToSelector:@selector(imageToolView:didConfirmText:)]) {
        [self.delegate imageToolView:self didConfirmText:self.attachTextField.text];
    }
}

//前进
- (IBAction)actionForward:(UIButton *)sender {
    
    if ([self.delegate respondsToSelector:@selector(imageToolView:styleStatusDidForward:)]) {
        [self.delegate imageToolView:self styleStatusDidForward:self.styleStatus];
    }
    
}

//后退
- (IBAction)actionBackward:(UIButton *)sender {
    
    if ([self.delegate respondsToSelector:@selector(imageToolView:styleStatusDidBackward:)]) {
        [self.delegate imageToolView:self styleStatusDidBackward:self.styleStatus];
    }
    
}






@end
